package controller;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;

import util.ContactsContainer;
import domain.ContactGroup;
import domain.itf.IDAOContact;
import domain.itf.IDAOContactGroup;

/**
 * Controller class for search a contact
 */
@Controller
@RequestMapping("/SearchContact")
public class SearchContactController {
	
	@Autowired
    ServletContext servletContext;

	private void addAllGroupsToView(HttpServletRequest request, ModelAndView view) {
		// get alls groups
		ApplicationContext context = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
		IDAOContactGroup daoContactGroup = (IDAOContactGroup) context.getBean("DAOContactGroup");
		List<ContactGroup> groups = daoContactGroup.getAllGroups();
		if (groups.isEmpty())
			view.addObject("groups", null);
		else
			view.addObject("groups", groups);
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView doGet(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("searchContact");
		this.addAllGroupsToView(request, view);
		return view;
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView doPost(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView("searchContact");
		this.addAllGroupsToView(request, view);
		
		// get the IDAOContact with spring
		ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		IDAOContact daoContact = (IDAOContact) context.getBean("DAOContact");
		ContactsContainer contacts = null;
		
		String searchType = request.getParameter("typeSearch");
		if (searchType.equals("groupSearch")) {
			String[] groups = request.getParameterValues("groups");
			try {
				Integer[] groupIds = new Integer[groups.length];
				for (int i = 0; i < groups.length; i++) {
					try {
						groupIds[i] = Integer.parseInt(groups[i]);
					} catch (NumberFormatException e) {}
				}
				contacts = daoContact.getByGroups(groupIds);
			} catch (Exception e) {
				e.printStackTrace();
				view.addObject("messageError", "Impossible to get the members.");
				return view;
			}
		} else if (searchType.equals("criteria")) {
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
			String order = request.getParameter("order").replace(" n", "N");
			String isAscendantString = request.getParameter("isAscendant");
			
			boolean isAscendant = isAscendantString.equals("ascendant");
			
			try {
				contacts = daoContact.searchContactsWithMultipleCriteria(firstName, lastName, email, isAscendant, order);
			} catch (Exception e) {
				e.printStackTrace();
				view.addObject("messageError", "Impossible to get the members.");
				return view;
			}
			
		}
		view.addObject("contacts", contacts);
		return view;
	}
}
